package com.example.sociamedia.user.mapper;


import com.example.sociamedia.user.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM users WHERE UserId = #{userid}")
    User selectUserById(@Param("userid") Long userid);

    @Select("SELECT * FROM users WHERE UserName = #{username}")
    User selectUserByName(@Param("username") String username);

    @Insert("INSERT INTO users (UserName, Email, PasswordHash, FullName, Bio, ProfilePictureUrl) VALUES (#{username}, #{email}, #{passwordhash}, #{fullname}, #{bio}, #{profilepictureurl})")
    @Options(useGeneratedKeys = true, keyColumn = "UserId", keyProperty = "userid")//生成一个自增的主键 这个主键是对应数据库中的哪个属性 把生成的这个值放到哪
    int insertUser(User user);

    @Update("UPDATE users SET UserName = #{username}, Email = #{email}, FullName = #{fullname}, Bio = #{bio}, ProfilePictureUrl = #{profilepictureurl} WHERE UserId = #{userid}")
    int updateUserProfile(User user);

    @Update("UPDATE users SET PasswordHash = #{newPasswordHash} WHERE UserId = #{userid}")
    int updatePassword(@Param("userid") Long userid, @Param("newPasswordHash") String newPasswordHash);

    @Update("UPDATE users SET SessionToken = #{sessionToken}, TokenExpiry = #{tokenExpiry} WHERE UserName = #{username}")
    int updateSessionToken(User user);

    @Select("SELECT * FROM users WHERE SessionToken = #{sessionToken}")
    User selectUserByToken(@Param("sessionToken") String sessionToken);

}